package net.wangds.conf.swagger;

import net.wangds.spring.utils.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.ServletContext;
import java.util.HashSet;
import java.util.Set;

//@Configuration
//@EnableSwagger2
@SuppressWarnings("unused")
public class SwaggerConfig {


    /**
     * 是否开启https.
     */
    private static final String ENV_PROP_NAME_OPT_ENABLE_HTTPS = "SWAGGER_ENABLE_HTTPS";

    /**
     * 访问的主机地址.
     * <P>格式：地址:端口</P>
     */
    private static final String ENV_PROP_NAME_OPT_SERVER_HOST = "SWAGGER_SERVER_HOST";
    /**
     * 应用上下文路径.
     */
    private static final String ENV_PROP_NAME_OPT_CONTEXT_PATH = "SWAGGER_CONTEXT_PATH";

    /**
     * API版本.
     */
    private static final String ENV_PROP_NAME_OPT_API_VERSION = "SWAGGER_API_VERSION";
    /**
     * 默认图片路径.
     */
    private static final String ENV_PROP_NAME_OPT_API_NO_PIC_URL = "SWAGGER_NOPIC_URL";
    /**
     * Swagger中项目标题。
     */
    private static final String ENV_PROP_NAME_OPT_SWAGGER_TITLE = "SWAGGER_SWAGGER_TITLE";
    /**
     * Swagger的项目说明.
     */
    private static final String ENV_PROP_NAME_OPT_SWAGGER_DESCCRIPTION = "SWAGGER_DESC";

    /**
     * Swagger中的团队支持url。
     */
    private static final String ENV_PROP_NAME_OPT_SWAGGER_TIME_OF_SERVVICE_URL = "SWAGGER_TEAM_OF_SERVICE_URL";

    private static final String ENV_PROP_NAME_OPT_SWAGGER_PACKAGE = "SWAGGER_PACKAGE";

    /**
     * 默认名称.
     */
    private static final String SWAGGER_DEFAULT_TITLE = "应用服务";

    private static final String PROTOCOL_HTTP = "http://";
    private static final String PROTOCOL_HTTPS = "https://";

    private static final String[] TEXT_YES = new String[]{"true", "t", "yes", "y", "ok", "是","1"};


    boolean enableHttps(){
        return StringUtils.equalsAnyIgnoreCase(SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_ENABLE_HTTPS, "y"), TEXT_YES);
    }



    String fetchHost(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_SERVER_HOST, "localhost");
    }

    String fetchContextPath(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_CONTEXT_PATH, "/");
    }

    String fetchSwaggerTitle(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_SWAGGER_TITLE, SWAGGER_DEFAULT_TITLE);
    }


    String fetchSwaggerDescription(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_SWAGGER_DESCCRIPTION, "");
    }

    String fetchSwaggerTeamServiceUrl(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_SWAGGER_TIME_OF_SERVVICE_URL, "");
    }

    String fetchApiVersion(){
        return SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_API_VERSION, "");
    }

    @Autowired
    ServletContext servletContext;

    @Bean
    public Docket api() {

        Set<String> protocols = new HashSet<>();
        protocols.add("http");
        if(enableHttps()) {
            protocols.add("https");
        }

        return new Docket(DocumentationType.SWAGGER_2)
                .protocols(protocols)
                .host(fetchHost())
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return fetchContextPath();
                    }
                })
                .select()//选择那些路径和api会生成document
                //.apis(RequestHandlerSelectors.any())//对所有api进行监控
                .apis(RequestHandlerSelectors.basePackage(SpringUtils.getSysEnvOrSpringEvnOption(ENV_PROP_NAME_OPT_SWAGGER_PACKAGE, "net.wangds")))
                .paths(PathSelectors.any())//对所有路径进行监控
                .build().apiInfo(new ApiInfoBuilder()
                        .title(fetchSwaggerTitle())
                        .description(fetchSwaggerDescription())
                        .termsOfServiceUrl(fetchSwaggerTeamServiceUrl())
                        .version(fetchApiVersion())
                        .build());
    }


}
